<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Common_Function.php");
require_once("../class/Core_Lib_Page.php"); //翻页

//访问权限验证
$userId = $_SESSION['user_id'];
$userRole = $_SESSION['user_role'];
if($userRole == 1 ){
	//管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}


//客户详情 
$id = $_POST['customerId'];
$sql = "SELECT * from customers where id=".$id;
$result = $db->Execute($sql);



$doFlag = $_POST["doFlag"];
if($doFlag == "addDetail"){
	$newDetail = $_POST['newDetail'];
	$sql_insert_customerdetail = "INSERT INTO customerdetail
							 (customer_id,customer_detail,create_time)
							 values
							 ($id,'$newDetail',Now())";
	$db->Execute($sql_insert_customerdetail);
}else if($doFlag == "updateDetail"){
	//不编辑,直接删除
}else if($doFlag == "deleteDetail"){
	$detailId =  $_POST["detailId"];
	$sqlDel = "delete from customerdetail where id= $detailId";
	$db->Execute($sqlDel);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "客户资料"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="detailForm" action="customerDetail.php" method="post">
<div id="container" >	
<div class="grayTable">	
<h4>
客户资料
</h4>
<table >
<thead>
	<tr>
		<th scope="col" width=90 >字段名称</th>
	    <th scope="col" >字段值</th>		
	</tr>
</thead>

<?php
$commonFunction = new Common_Function();
if($id == ""){
	echo "Error: Can't get customer ID.";
}else{	
	if(!$result->EOF){
		$strTR = "";
		$strTR = $strTR."<tr ><td>ID</td>";
		$strTR = $strTR."<td>".$result->fields['id']."</td></tr>";
		$strTR = $strTR."<tr ><td>Create Time</td>";
		$strTR = $strTR."<td>".$result->fields['create_time']."</td></tr>";
		$strTR = $strTR."<tr ><td>First Name</td>";
		$strTR = $strTR."<td>".$result->fields['firstname']."</td></tr>";
		$strTR = $strTR."<tr ><td>Last Name</td>";
		$strTR = $strTR."<td>".$result->fields['lastname']."</td></tr>";
		$strTR = $strTR."<tr ><td>Email</td>";
		$strTR = $strTR."<td>".$result->fields['email_address']."</td></tr>";
		$strTR = $strTR."<tr ><td>Street Address</td>";
		$strTR = $strTR."<td>".$result->fields['street_address']."</td></tr>";
		$strTR = $strTR."<tr ><td>City</td>";
		$strTR = $strTR."<td>".$result->fields['city']."</td></tr>";
		$strTR = $strTR."<tr ><td>State</td>";
		$strTR = $strTR."<td>".$result->fields['state']."</td></tr>";
		$strTR = $strTR."<tr ><td>Country</td>";
		$strTR = $strTR."<td>".$result->fields['country']."</td></tr>";
		$strTR = $strTR."<tr ><td>Post code</td>";
		$strTR = $strTR."<td>".$result->fields['postcode']."</td></tr>";
		$strTR = $strTR."<tr ><td>Telephone Number</td>";
		$strTR = $strTR."<td>".$result->fields['tel_number']."</td></tr>";
		$strTR = $strTR."<tr ><td>VIP Level</td>";
		$strTR = $strTR."<td>".$commonFunction->getVIPLevel($vip_level)."</td></tr>";
		$strTR = $strTR."<tr ><td>Site Name</td>";
		$strTR = $strTR."<td>".$result->fields['site_name']."</td></tr>";
		$strTR = $strTR."<tr ><td>Produc Name</td>";
		$strTR = $strTR."<td>".$result->fields['product_name']."</td></tr>";
		$strTR = $strTR."<tr ><td>Note</td>";
		$strTR = $strTR."<td><div id='textareaContent'>".$result->fields['note']."</div></td></tr>";

		$strTR = $strTR."<tr><td colspan=2 align=center>";
		$strTR = $strTR."<input type='button' value='编辑' onclick='doEdit(".$id.")'>";
		$strTR = $strTR."&nbsp;&nbsp;&nbsp;&nbsp;";
		$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
		$strTR = $strTR."</td></tr>";
		echo $strTR;
	}
}
?>

</table>
</div>


<br>


<!--客户跟单详情------------------------->
<div>
<h4>客户订单详情</h4>
<div class="leftDiv"><textarea name="newDetail" id="newDetail" rows=5 cols=60></textarea></div>
<div class="leftDiv"> <br>&nbsp;&nbsp;<a href="javascript:addDetail()" class="button icon add">新加内容</a></div>
<div class="clearBoth">&nbsp;</div>

<div class="grayTable">	
<table >
<thead>
	<tr>
		<th scope="col" width=100>时间</th>
	    <th scope="col" >内容</th>	
		<th scope="col" width=50>操作</th>	
	</tr>
</thead>

<?php

if($id == ""){
	echo "Error: Can't get customer ID.";
}else{	
//--------------------------------------------------------------------------------
	$listFlag = $_POST["listFlag"];
	$list_rows = 50; //每页显示的记录数
	$sql_all = "SELECT COUNT(*) as total_rows from customerdetail where customer_id=".$id;	
	$result = $db->Execute($sql_all);
	while (!$result->EOF) {
		$totalRows = $result->fields['total_rows'];
		$result->MoveNext();
	}
	$nowPage = $_POST['nowPage'];
	$nowPage = !empty($nowPage) ? $nowPage : 1;
	$params = array(
		'total_rows'=>$totalRows,
		'list_rows'=>$list_rows,
		'method'    =>'ajax',
		'ajax_func_name' =>'goToPage',
		'now_page' => $nowPage 
	);
	$page = new Core_Lib_Page($params);
	$limit_m = $page->first_row;
	$limit_n = $list_rows;

	$sql = "SELECT * from customerdetail where customer_id=".$id." order by create_time desc limit $limit_m,$limit_n";//default all
	if($doFlag == "toList"){//翻页
		if($listFlag == "1"){ //全部
			$sql = "SELECT * from customerdetail where customer_id=".$id." order by create_time desc limit $limit_m,$limit_n";
		}else if($listFlag == "2"){ //搜索
			//
		}
	}
	$resultDetail = $db->Execute($sql);
	while (!$resultDetail->EOF) {
		$customerdetail_id = $resultDetail->fields['id'];
		$create_time = $resultDetail->fields['create_time'];
		$customer_detail = $resultDetail->fields['customer_detail'];
		$strTR = "";
		$strTR = $strTR."<tr >";
		$strTR = $strTR."<td>".$create_time."</td>";
		$strTR = $strTR."<td><div id='textareaContent_".$customerdetail_id."'>".$customer_detail."</div></td>";
		$strTR = $strTR."<script>$('#textareaContent_".$customerdetail_id."').html(replaceEnter2BR($('#textareaContent_".$customerdetail_id."').html()));</script>";
		$strTR = $strTR."<td><input type='button' value='删除' onclick='doDeleteDetail(".$customerdetail_id.")' class='button icon remove'></td>";
		$strTR = $strTR."</tr>";
		echo $strTR;
		$resultDetail->MoveNext();	
	}
}
?>

</table>

<br>

<!--Page 分页,翻页 -->
<div id="page"><?php echo $page->show(2);?></div>


</div>



<br><br>
</div><!--end container -->

<br><br>
<br><br>

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="customerId" id="customerId" value="<?php echo $id;?>" >
<input type="hidden" name="detailId" id="detailId" >
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>"><!--翻页-->
</form>


<form name="inputForm" action="customerInput.php" method="post">
<input type="hidden" name="editId" id="editId" >
</form>



</body>




</html>

<script>

//textarea 内容分行显示
$("#textareaContent").html(replaceEnter2BR($('#textareaContent').html()));


function toList(){
	window.location.href = "customerList.php";
}

function addDetail(){
	document.getElementById('doFlag').value = 'addDetail';	
	document.detailForm.submit();
}

function doEdit(id){
	document.getElementById('editId').value = id;
	document.inputForm.submit();
}


function doDeleteDetail(id){
	if(confirm("是否删除这条记录,删除后不可恢复,是否继续?")){ 
		document.getElementById('doFlag').value = "deleteDetail";
		document.getElementById('detailId').value = id;
		document.detailForm.submit();
	}else{
		return;
	}
}


/**翻页**/
function goToPage(nowPage){
	document.getElementById('doFlag').value = 'toList';
	document.getElementById('nowPage').value = nowPage;
	document.detailForm.submit();
}



</script>

